package com.starbaba.mainicon.view;

import java.io.IOException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.annotation.Resource;

import com.google.gson.JsonObject;
import com.opensymphony.xwork2.Action;
import com.opensymphony.xwork2.ModelDriven;
import com.starbaba.common.ResultJson;
import com.starbaba.core.utils.PropertyParamMapBuilder;
import com.starbaba.core.utils.Struts2Utils;
import com.starbaba.core.utils.json.JsonUtils;
import com.starbaba.core.web.page.PageResponse;
import com.starbaba.core.web.struts2.BaseAction;
import com.starbaba.core.web.translate.TranslateVO;
import com.starbaba.mainbanner.info.model.ProductChannelVO;
import com.starbaba.mainicon.model.HomeBannerModel;
import com.starbaba.mainicon.model.HomeBannerVO;
import com.starbaba.mainicon.service.HomeBannerService;

@SuppressWarnings({"rawtypes","unchecked"})
public class HomeBannerAction extends BaseAction implements ModelDriven{
	
    private static final long serialVersionUID = 1L;

    private HomeBannerModel model;
	
	@Resource(name="homeBannerServiceImpl")
	private HomeBannerService serviceForList;
	
	@Override
	public String show() {
		paramsMap = PropertyParamMapBuilder.buildFromHttpRequest(Struts2Utils.getRequest(), paramsMap);
		super.show();
		return Action.SUCCESS;
	}

	@Override
	protected void beforeShow() throws Exception {
		ArrayList<TranslateVO> channels = getChannel();
		paramsMap.put("channelList", channels);
	}
    
	//查询列表的方法
    public void listApp(){
		try {
			paramsMap = PropertyParamMapBuilder.buildFromHttpRequest(Struts2Utils.getRequest(), paramsMap);
			String platform = (String) paramsMap.get("platform");
			String channel = (String) paramsMap.get("channel");
			String status = (String) paramsMap.get("status");
			String type = (String) paramsMap.get("type");
			if(null != platform){
				model.setPlatform(Integer.valueOf(platform));
			}
			if(null != channel){
				model.setChannel(channel);
			}
			if(null != status){
				model.setStatus(Integer.valueOf(status));
			}
			if(null != type){
				model.setType(Integer.valueOf(type));
			}
			parsePageRequest();
			PageResponse pageResponse = new PageResponse();
			List list = serviceForList.getDataList(pageRequest.getIDisplayStart() , pageRequest.getIDisplayLength(),model,pageResponse,getService());
			pageResponse.setAaData(list);
			pageResponse.setSEcho(pageRequest.getSEcho());
			String jsonStr = JsonUtils.toJson(pageResponse, null, true, null, false);
			sendJsonMessage(jsonStr);
			
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	//新增的方法
	public void addApp() throws ParseException{
		paramsMap = PropertyParamMapBuilder.buildFromHttpRequest(Struts2Utils.getRequest(), paramsMap);
		String channel = (String) paramsMap.get("schannel");
		String platform = (String) paramsMap.get("splatform");
		String imgurl = (String) paramsMap.get("simgurl");
		String pkgname = (String) paramsMap.get("spkgname");
		String end_time = (String) paramsMap.get("send_time");
		String content = (String) paramsMap.get("scontent");
		String type = (String) paramsMap.get("stype");
		String launch_params = (String) paramsMap.get("launch_params");
		
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");  
		
		Date realEnd_time = null;
		if(null != end_time){
			realEnd_time = sdf.parse(end_time); 
		} 
		
		try{
			HomeBannerVO entity = new HomeBannerVO();
			entity.setChannel(channel);
			entity.setImgurl(imgurl);
			entity.setPkgname(pkgname);
			entity.setPlatform(Integer.valueOf(platform));
			entity.setLaunch_params(launch_params);
			entity.setContent(content);
			entity.setType(Integer.valueOf(type));
			entity.setStatus(1);
			entity.setEnd_time(realEnd_time);
			
			this.getService().doCreate(HomeBannerVO.class, entity);
			
			JsonObject result = new JsonObject();
			ResultJson resultJs = new ResultJson();
			resultJs.msg = "新增成功";
			result.add("result", resultJs.toJSON());
			sendJsonMessage(result.toString());
			
		}catch(Exception e){
			sendErrMsg(e.getMessage());
		}
	}
	
	//修改的方法(包含上下线)
	public void updateApp() throws ParseException{
		paramsMap = PropertyParamMapBuilder.buildFromHttpRequest(Struts2Utils.getRequest(), paramsMap);
		String id = (String) paramsMap.get("id");
		String channel = (String) paramsMap.get("schannel");
		String platform = (String) paramsMap.get("splatform");
		String imgurl = (String) paramsMap.get("simgurl");
		String pkgname = (String) paramsMap.get("spkgname");
		String end_time = (String) paramsMap.get("send_time");
		String launch_params = (String) paramsMap.get("launch_params");
		//1为普通修改,2为上下线
		String status = (String) paramsMap.get("status");
		String type = (String) paramsMap.get("type");
		String stype = (String) paramsMap.get("stype");
		String content = (String) paramsMap.get("scontent");
		
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");  
		
		Date realEnd_time = null;
		if(null != end_time){
			realEnd_time = sdf.parse(end_time); 
		} 
		
		try{
			if(Integer.valueOf(type) == 1){
				this.getService().executeHQLUpdate(" update HomeBannerVO as vo set vo.channel = ?, vo.platform = ?, vo.imgurl = ?, vo.pkgname = ?, vo.end_time = ?, vo.content = ?, vo.type = ?, vo.launch_params = ? where id = ?",
						new Object[]{channel, Integer.valueOf(platform), imgurl, pkgname, realEnd_time, content, Integer.valueOf(stype), launch_params, Long.valueOf(id)});
			}else if(Integer.valueOf(type) == 2){
				this.getService().executeHQLUpdate(" update HomeBannerVO as vo set vo.status = ? where id = ?",
						new Object[]{Integer.valueOf(status), Long.valueOf(id)});
			}
			JsonObject result = new JsonObject();
			ResultJson resultJs = new ResultJson();
			resultJs.msg = "修改成功";
			result.add("result", resultJs.toJSON());
			sendJsonMessage(result.toString());
			
		}catch(Exception e){
			sendErrMsg(e.getMessage());
		}
	}
	
	//删除的方法
	public void deleteApp(){
		paramsMap = PropertyParamMapBuilder.buildFromHttpRequest(Struts2Utils.getRequest(), paramsMap);
		String id = (String) paramsMap.get("id");
		try{
			HomeBannerVO entity = new HomeBannerVO();
			entity.setId(Long.valueOf(id));
			this.getService().doRemoveByVO(HomeBannerVO.class, entity);

			JsonObject result = new JsonObject();
			ResultJson resultJs = new ResultJson();
			resultJs.msg = "删除成功";
			result.add("result", resultJs.toJSON());
			sendJsonMessage(result.toString());
			
		}catch(Exception e){
			sendErrMsg(e.getMessage());
		}
	}
	
	//排序的方法
	public void sortApp() {
		paramsMap = PropertyParamMapBuilder.buildFromHttpRequest(Struts2Utils.getRequest(), paramsMap);
		String sortlist = (String) paramsMap.get("sortlist");
		String[] appidList = sortlist.split("#");
		parsePageRequest();
		String hql = "Update HomeBannerVO vo set vo.orderby = ? where vo.id = ?";
		try {
			for (int index = 0; index < appidList.length; ++index) {
				Long appId = Long.parseLong(appidList[index]);
				this.getService().executeHQLUpdate(hql, index, appId);
			}

			JsonObject result = new JsonObject();
			ResultJson resultJs = new ResultJson();
			resultJs.msg = "排序成功";
			result.add("result", resultJs.toJSON());
			sendJsonMessage(result.toString());
		} catch (Exception e) {
			sendErrMsg(e.getMessage());

		}
	}

	
	//查询渠道的方法
	public ArrayList<TranslateVO> getChannel() throws Exception {
		List<ProductChannelVO> list = (List<ProductChannelVO>) this.getService().doFindALL(ProductChannelVO.class);
		ArrayList<TranslateVO> channels = new ArrayList<TranslateVO>();
		for (ProductChannelVO vo : list) {
			channels.add(new TranslateVO(vo.getChannelCode().toString(), vo.getChannelName()));
		}
		return channels;
	}
	
	@Override
	public Object getModel() {
		if(model == null)
		{
			model = new HomeBannerModel();
		}
		return model;
	}
	
	private void sendErrMsg(String msg) {
		JsonObject result = new JsonObject();
		ResultJson resultJs = new ResultJson();
		resultJs.msg = msg;
		result.add("result", resultJs.toJSON());
		try {
			sendJsonMessage(result.toString());
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
}
